package com.newtype.meeting.client.widget.frontend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.shared.MeetingNotifyDTO;

public class MeetingNoticeTabPanel extends ContentPanel {
	private ColumnModel cm;

	public MeetingNoticeTabPanel(ListStore<MeetingNotifyDTO> store) {
		setLayout(new FitLayout());
		setHeaderVisible(false);
		setFrame(true);

		GridCellRenderer<MeetingNotifyDTO> subject = new GridCellRenderer<MeetingNotifyDTO>() {
			@Override
			public Object render(MeetingNotifyDTO model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<MeetingNotifyDTO> store,
					Grid<MeetingNotifyDTO> grid) {
				Date readDate = (Date) model.getReadDate();
				String val = (String) model.get(property);
				String str = "<span style='background-color:#CE0000; color:white; padding:1px 5px 1px 5px; margin-left:5px;'>待回复</span>";
				if (readDate == null) {
					return str + "&nbsp;<span style='font-weight: bold;'>"
							+ val + "</span>";
				} else {
					return str + "&nbsp;<span>" + val + "</span>";
				}
			}
		};

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		ColumnConfig column = new ColumnConfig("originatorName", "发起人", 100);
		column.setRowHeader(true);
		configs.add(column);

		column = new ColumnConfig("chairName", "主席", 100);
		configs.add(column);

		column = new ColumnConfig("subject", "主题", 400);
		column.setRenderer(subject);
		configs.add(column);

		column = new ColumnConfig("startDate", "会议时间", 175);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		configs.add(column);

		cm = new ColumnModel(configs);

		Grid<MeetingNotifyDTO> grid = new Grid<MeetingNotifyDTO>(store, cm);
		grid.setAutoExpandColumn("subject");
		grid.setBorders(false);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		grid.setStateful(true);

		grid.setStateful(true);
		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<MeetingNotifyDTO>>() {
					public void handleEvent(
							SelectionChangedEvent<MeetingNotifyDTO> be) {
						if (be.getSelection().size() > 0) {
							MeetingNotifyDTO notifyDTO = be.getSelection().get(
									0);
							Dispatcher.forwardEvent(AppEvents.ToMeetingNotice,
									notifyDTO);
						}
					}
				});

		ContentPanel cp = new ContentPanel();
		cp.setBodyBorder(false);
		cp.setHeaderVisible(false);
		cp.setLayout(new FitLayout());
		cp.setHeight(205);
		cp.add(grid);

		VerticalPanel vp = new VerticalPanel();
		vp.setLayout(new FlowLayout(5));
		vp.setBorders(false);
		vp.setHeight(250);

		vp.add(createTitle("会议通知"));

		vp.add(cp);
		add(vp);
	}

	private Text createTitle(String str) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div style='line-height: 2em;'>");
		sb.append("<p>");
		sb.append("<img src='" + GWT.getHostPageBaseURL()
				+ "images/Meeting.png'>");
		sb
				.append("<span  style='font-size:22px;font-weight: bold;color:#0060A0;'>"
						+ str + "</span>");
		sb.append("</p>");
		sb.append("<hr size='1'>");
		sb.append("</div>");
		Text title = new Text(sb.toString());
		return title;
	}
}
